package com.netease.homework.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.netease.homework.dao.IUserDao;
import com.netease.homework.meta.User;
import com.netease.homework.service.IUserService;

@Component
@Qualifier("UserService")
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao userDao;
	
	
    
	

	@Override
	public Map<String, Object> getUserById(Integer id) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		User user = userDao.getUserById(id);

		if (user != null) {
			returnMap.put("value", user);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> getUserByName(String name) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		User user = userDao.getUserByName(name);

		if (user != null) {
			returnMap.put("value", user);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> addUser(String name, String password) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		boolean result = userDao.save(new User(name, password));

		if (result) {
			returnMap.put("value", result);
			returnMap.put("message", "新增成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}
		

		return returnMap;
	}

}
